import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class EchoServer {
	public static void main(String[] args) {
		try (ServerSocket serverSocket = new ServerSocket(31038);
				Socket clientSocket = serverSocket.accept();
				OutputStream out = clientSocket.getOutputStream();
				InputStream in = clientSocket.getInputStream();) {
			byte[] b = new byte[1];

			// echo everything back
			while (true) {
				while (in.read(b, 0, 1) == 1) {
					System.out.println("read in" + b[0]);
					out.write(b[0]);
				}
			}

		} catch (IOException e) {
			System.out
					.println("Exception caught when trying to listen on port "
							+ "31038" + " or listening for a connection");
			System.out.println(e.getMessage());
		}
	}
}
